package com.sz.biz.pay.transfer.service.impl;

import com.sz.biz.pay.transfer.service.BankCallBackServiceService;
import com.sz.biz.pay.transfer.service.NotifyService;
import com.sz.common.pay.common.PayLogger;
import com.sz.common.pay.common.SequenceGenerator;
import com.sz.common.pay.pingan.constant.Constant;
import com.sz.common.pay.pingan.constant.ResponseCode;
import com.sz.common.pay.pingan.core.server.PAServer;
import com.sz.common.pay.pingan.entity.common.CommonHeadResponse;
import com.sz.common.pay.pingan.exception.PayException;
import com.sz.common.pay.pingan.util.PayExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Function: 银行通过http请求业务系统 <br>
 *
 * @author: siqishangshu <br>
 * @date: 2017-12-04 18:54:00
 * <p>
 * 处理未完成的支付，提现等业务，
 * 首先找到上次未完成交易的数据，然后再次提交给银行。
 * 注意状态与流水
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class NotifyServiceImpl implements NotifyService {

    private static PayLogger payLogger = PayLogger.getLogger(NotifyServiceImpl.class);

    @Autowired
    BankCallBackServiceService bankCallBackService;


    @Override
    public HttpServletResponse notifyByPost(HttpServletRequest request, HttpServletResponse response) {
        String sequence = SequenceGenerator.getThirdLogNo();
        if (!PAServer.VerifyLegitimateIP(request)) {
//            response.setStatus(401);
//            return response;
        }
        CommonHeadResponse headResponse = new CommonHeadResponse(sequence);
        Object responseObj = null;
        int tranFunc = 0;
        try {
             String tranMessage = PAServer.readRequest(sequence, request);
            tranFunc = PAServer.getTranFunCode(sequence, tranMessage);
            headResponse.setTranFunc(String.valueOf(tranFunc));
            switch (tranFunc) {
                case 1303:
                    //会员签解约维护【1303】
                    responseObj = bankCallBackService.handle1303(sequence, tranMessage);
                    break;
                case 1310:
                    //入金（银行发起）【1310】
                    responseObj = bankCallBackService.handle1310(sequence, tranMessage);
                    break;
                case 1312:
                    //出金（银行发起）【1312】
                    responseObj = bankCallBackService.handle1312(sequence, tranMessage);
                    break;
                case 1326:
                    //银行复核通知【1326】
                    responseObj = bankCallBackService.handle1326(sequence, tranMessage);
                    break;
                default:
                    PayExceptionUtil.throwPayCommonException("不支持的交易类型", ResponseCode.ERR935);
            }
            headResponse.succeed();
            if (responseObj == null) {
                headResponse.failed();
                responseObj = headResponse;
            }
            PAServer.writeResponse(sequence, responseObj, response, headResponse);
        } catch (PayException e) {
            try {
                headResponse.failed(e.getErrorMessage());
                PAServer.writeResponse(sequence, responseObj, response, headResponse);
                payLogger.error(sequence, e.getErrorMessage().toString());
            } catch (PayException e1) {
                response.setStatus(Constant.SERVER_ERROR);
            }
        }catch (Exception e){
            try {
                headResponse.failed();
                PAServer.writeResponse(sequence, responseObj, response, headResponse);
                payLogger.error(sequence, e.getMessage());
            } catch (PayException e1) {
                response.setStatus(Constant.SERVER_ERROR);
            }
        }
        payLogger.info(sequence, response.getStatus() + " ");
        return response;
    }


}
